package cn.wolfcode.shop.msg;

import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.vo.CreateOrderVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;

@Component
public class OrderConsumer {
    @Autowired
    private IOrderInfoService orderInfoService;

    @JmsListener(destination = "shop.order", containerFactory = "jmsListenerContainerQueue")
    public void receiveMessage(Message message) throws JMSException {
        if (message instanceof MapMessage) {
            MapMessage msg = (MapMessage) message;
            Long userId = msg.getLong("userId");
            String createOrderVoStr = msg.getString("createOrderVo");
            CreateOrderVo createOrderVo = JSON.parseObject(createOrderVoStr, CreateOrderVo.class);
            orderInfoService.generateOrder(userId, createOrderVo);
        }
    }
}
